<template>
	<div class="system-user-dialog-container">
		<el-dialog :title="state.dialog.title" v-model="state.dialog.isShowDialog" width="769px">
			<el-form ref="userDialogFormRef" :model="state.ruleForm" size="default" label-width="90px">
				<el-row :gutter="35">
					<el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12" class="mb20">
						<el-form-item label="账户名称">
							<el-input v-model="state.ruleForm.username" placeholder="字母|数字开头包含(@ . _) 4-16位" clearable :disabled="state.dialog.type==='edit'"></el-input>
						</el-form-item>
					</el-col>
					<el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12" class="mb20">
						<el-form-item label="用户昵称">
							<el-input v-model="state.ruleForm.nickname" placeholder="请输入用户昵称" clearable></el-input>
						</el-form-item>
					</el-col>
					<el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12" class="mb20">
						<el-form-item label="关联角色">
							<el-select v-model="state.ruleForm.roleId" placeholder="请选择" multiple clearable class="w100" collapse-tags="true">
								<el-option v-for="item in state.roles"
								:key="item.name"
								:label="item.name"
								:value="item.id"></el-option>
							</el-select>
						</el-form-item>
					</el-col>
					<el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12" class="mb20">
						<el-form-item label="手机号">
							<el-input v-model="state.ruleForm.mobile" placeholder="请输入手机号" clearable></el-input>
						</el-form-item>
					</el-col>
					<el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12" class="mb20">
						<el-form-item label="邮箱">
							<el-input v-model="state.ruleForm.email" placeholder="请输入" clearable></el-input>
						</el-form-item>
					</el-col>
					<el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12" class="mb20">
						<el-form-item label="账户密码">
							<el-input v-model="state.ruleForm.password" placeholder="包含(字母、数字、@、.、_) 6-16位" type="password" clearable show-password></el-input>
						</el-form-item>
					</el-col>
					<el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12" class="mb20">
						<el-form-item label="用户状态">
							<el-switch v-model="state.ruleForm.status" active-value="1" inactive-value="0" inline-prompt active-text="启" inactive-text="禁"></el-switch>
						</el-form-item>
					</el-col>
				</el-row>
			</el-form>
			<template #footer>
				<span class="dialog-footer">
					<el-button @click="onCancel" size="default">取 消</el-button>
					<el-button type="primary" @click="onSubmit" size="default">{{ state.dialog.submitTxt }}</el-button>
				</span>
			</template>
		</el-dialog>
	</div>
</template>

<script setup lang="ts" name="systemUserDialog">
import { reactive, ref } from 'vue';
import { useUserApi } from '/@/api/system/user';
import { ElMessage } from 'element-plus';

// 定义子组件向父组件传值/事件
const emit = defineEmits(['refresh']);

// 定义变量内容
const userDialogFormRef = ref();
const state = reactive({
	ruleForm: {
		id:0,//用户id
		username: '', // 账户名称
		nickname: '', // 用户昵称
		roleId: [], // 关联角色
		mobile: '', // 手机号
		email: '', // 邮箱
		password: '', // 账户密码
		status: '1', // 用户状态
	},
	dialog: {
		isShowDialog: false,
		type: '',
		title: '',
		submitTxt: '',
	},
	roles: [] as any,//角色数组
	defaultForm: {
		id:0,//用户id
		username: '', // 账户名称
		nickname: '', // 用户昵称
		roleId: [], // 关联角色
		mobile: '', // 手机号
		email: '', // 邮箱
		password: '', // 账户密码
		status: '1', // 用户状态
	},
});
//获取角色数组
const getRoleData = () => {
	if(state.roles.length){
		return !1;
	}
	useUserApi().getAdminRole().then(res=>{
		if(!res.code){
			state.roles = res.data;
		}
	}).catch(err=>{
		console.log(err);
	});
};

// 打开弹窗
const openDialog = (type: string, row: any) => {
	if (type === 'edit') {
		
		var info = row;
		info = getFormData(info);
		info.status = String(info.status);
		
		state.ruleForm = info;
		state.dialog.title = '修改用户';
		state.dialog.submitTxt = '修 改';
		state.dialog.type = 'edit';
	} else {
		state.dialog.title = '新增用户';
		state.dialog.submitTxt = '新 增';
		state.dialog.type = 'add';
		// 清空表单
		state.ruleForm = state.defaultForm;
	}
	state.dialog.isShowDialog = true;
	getRoleData();
};
//填充提交数据
const getFormData = (data: any) => {
	var newData = {} as any,i;
	for(i in state.defaultForm){
		newData[i] = data[i];
	}

	return newData;
}
// 关闭弹窗
const closeDialog = () => {
	state.dialog.isShowDialog = false;
};
// 取消
const onCancel = () => {
	closeDialog();
};
// 提交
const onSubmit = () => {
	
	useUserApi().setAdminUser(state.ruleForm).then(res=>{
		if(!res.code){
			ElMessage.success(state.dialog.title+'成功');
		}
	}).catch(err=>{
		console.log(err);
	});
	// 清空表单
	state.ruleForm = state.defaultForm;
	closeDialog();
	emit('refresh');
	// if (state.dialog.type === 'add') { }
};

// 暴露变量
defineExpose({
	openDialog,
});
</script>
